package com.durablemethods.futures.server;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import com.durablemethods.futures.client.NHLTradeEntryDto;
import com.durablemethods.futures.client.NHLTrader;
import com.google.appengine.api.rdbms.AppEngineDriver;

public class StaticTradeService {
	private static final String CONNECTION_STRING = "jdbc:google:rdbms://durablemethods.com:duravest:durable-futures-db";
	private Logger log = Logger.getLogger(StaticTradeService.class.getName());
	public List<NHLTradeEntryDto> getTrades(){
		Map<Integer,NHLTradeEntryDto> processMap = new HashMap<Integer, NHLTradeEntryDto>();
		List<NHLTradeEntryDto> returnList = new  ArrayList<NHLTradeEntryDto>();
		Connection c = null;
		try {
			DriverManager.registerDriver(new AppEngineDriver());
			c = DriverManager.getConnection(CONNECTION_STRING,"root","");
			PreparedStatement ps = c.prepareStatement("select t.ID, t.TRANSACTION_DATE, t.TEAM_ID,t.QUANTITY, t.PRICE, tr.google_account,tr.alias from durable_futures.TRADES t, durable_futures.TRADERS tr where tr.google_account = t.trader_id and t.status not in ('I', 'C') ");
			ResultSet rs = ps.executeQuery();
			while (rs.next()){
				int tradeId = rs.getInt("ID");
				int quantity = rs.getInt("quantity");
				double price = rs.getDouble("price");
				NHLTrader trader = new NHLTrader();
				trader.setAlias(rs.getString("alias"));
				trader.setGoogleAccount(rs.getString("google_account"));
				if (processMap.containsKey(tradeId)){
					NHLTradeEntryDto nhlTradeEntryDto = processMap.get(tradeId);
					if (quantity<0){
						nhlTradeEntryDto.setSeller(trader);
					}else{
						nhlTradeEntryDto.setBuyer(trader);						
					}
				}else{
					NHLTradeEntryDto nhlTradeEntryDto = new NHLTradeEntryDto();
					nhlTradeEntryDto.setPrice(price);
					if (quantity<0){
						nhlTradeEntryDto.setSeller(trader);
						nhlTradeEntryDto.setShares(quantity*-1);
					}else{
						nhlTradeEntryDto.setBuyer(trader);
						nhlTradeEntryDto.setShares(quantity);
					}
					nhlTradeEntryDto.setId(tradeId);
					nhlTradeEntryDto.setTeamId(rs.getString("TEAM_ID"));
					nhlTradeEntryDto.setTransactionDate(rs.getTimestamp("TRANSACTION_DATE"));
					processMap.put(tradeId, nhlTradeEntryDto);
				}
			}
			ps.close();
			c.close();
		} catch (SQLException e) {
			log.severe(e.getMessage());
			e.printStackTrace();
		}
		returnList.addAll(processMap.values());
		Collections.sort(returnList, new TradeDTOSorter());
		return returnList;
	}
	class TradeDTOSorter implements Comparator<NHLTradeEntryDto>{

		@Override
		public int compare(NHLTradeEntryDto o1, NHLTradeEntryDto o2) {
			return o2.getTransactionDate().compareTo(o1.getTransactionDate());
		}
		
	}
	
	

}
